import 'dart:developer' as developer;
import 'package:ai_model/app/global/theme/app_theme.dart';
import 'package:ai_model/app/routes/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'app/core/network/core/api_client.dart';

// 自定义的全局日志函数
void logDebug(String tag, dynamic message) {
  // 标准输出
  print('[$tag] $message');

  // 使用dart:developer记录，可在DevTools中查看
  developer.log(
    message.toString(),
    name: tag,
    time: DateTime.now(),
  );
}

void logInfo(String message) {
  print(message);
}


// 全局导航键，用于BldFerrisWheel初始化
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Future<void> main() async {
  // 确保Flutter绑定已初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 设置系统UI样式 - 透明状态栏
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark,
  ));


  // 初始化ApiClient并设置为生产环境
  ApiClient().setEnvironment(AppEnvironment.test);


  // 运行应用
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(414, 896), // dp dimensions from PRD
      builder: (context,child) {
        return GetMaterialApp(
          title: 'AI伴侣',
          debugShowCheckedModeBanner: false,
          theme: AppTheme.light,
          initialRoute: AppPages.INITIAL,
          getPages: AppPages.routes,
          navigatorKey: navigatorKey,
          // 使用全局导航键
          builder:  (context, child) {
            // 确保所有页面都使用透明状态栏
            SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              statusBarIconBrightness: Brightness.dark,
            ));
            return child!;
          },
          // 设置默认语言为中文
          locale: const Locale('zh', 'CN'),
          fallbackLocale: const Locale('zh', 'CN'),
          // 添加 Flutter 本地化支持
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('zh', 'CN'),
          ],
        );
      },
    );
  }
}
